<?php

/*
 * Product Class
 * ===============================================
 * Date: March 17th, 2012
 * Created By: Integration Team
 * ===============================================
 */

/**
 * contains all the specific Etilize
 * information for a particular product
 *
 * @author Integration Team
 */
class product {
    
    
    protected $productInformation = array();            //holds all the product specs
    protected $categoryDisplayAttributes = array();     //holds the category display attributes template for the product
    protected $flatCategoryDisplayAttributes = array(); //holds a flat array of the category display attributes
    protected $productId;                               //Current product's productId
    protected $localeId;                                //Current product's productId
    protected $templateId;                              //The templateID as per the config for the datasheet
    
    protected $includeDescriptions;                     //True/False whether to include descriptions (only applies when $autoPopulate is True)
    protected $includeKeywords;                         //True/False whether to include Keywords (only applies when $autoPopulate is True)
    protected $includeResources;                        //True/False whether to include resources (only applies when $autoPopulate is True)
    protected $includeUpsellProducts;                   //True/False whether to include product Upsells (only applies when $autoPopulate is True)
    protected $includeSimilarProducts;                  //True/False whether to include Similar products (only applies when $autoPopulate is True)
    protected $includeAccessoryProducts;                //True/False whether to include Accesssory products (only applies when $autoPopulate is True)
    
    protected $upsellFormatted;                         //Holds the contents of the marked up Upsell values and is set by set_upsell_products
    protected $similarFormatted;                        //Holds the contents of the marked up Upsell values and is set by set_upsell_products
    protected $accessoriesFormatted;                    //Holds the contents of the marked up Upsell values and is set by set_upsell_products
    protected $attribute_data = array();                //Holds a copy of all the attributes replacing the productinfo - This is used to return values to get_attribute
    protected $descriptions = array();                  //Holds the product descriptions 
    protected $resources = array();                     //Holds the product images / resources 
    protected $keywords = "";                           //Holds the product keywords 


    protected $techspecs = "";                          //This will be used to hold the formatted technical specs - it's set by set_techspecs
    protected $overview = "";                           //This will be used to hold the formatted overview - it's set by set_techspecs
    protected $local_data = array();                    //Holds any dynamically passed data - This will be used to set any non-Etilize data on the fly
    
    
    public function __construct($productId=0,$cda=array(),$autoPopulateProductInfo=False,$descriptions=true,$keywords=true,$resources=true, $upsells=True,$similar=True,$accessories=True) {
        /**
         *
         * Retreive and set all the product information (Technical specs, images, descriptions)
         * @param    $productid = product for which information needs to be collected
         * @param    $cda = The category display template for the product - This will be used for sorting the technical specs
         * @param    $autoPopulateProductInfo - Set to true if the product information needs to be assembled from the database 
         *                                      as opposed to being passed by the caller 
         * @param    $descriptions = set to true if the descriptions need to be pulled - only applies if autoPopulate = true
         * @param    $keywords = set to true if the keywords need to be pulled - only applies if autoPopulate = true
         * @param    $resources = set to true if the resources need to be pulled - config will be checked for RC or basic - only applies if autoPopulate = true
         * @param    $upsells = set to True if upsell products need to be populated - config will be checked for number of Upsells - only applies if autoPopulate = true
         * @param    $similar = set to True if similar products need to be populated - config will be checked for number of Similar products - only applies if autoPopulate = true
         * @param    $Accessories = set to True if Accessory products need to be populated - config will be checked for number of Accessory products - only applies if autoPopulate = true
         
         * @return   Object - test
         *
         */
        $this->productId = $productId;
        $this->localeId = Locale;
        $this->templateId = Product_Template;
        $this->includeDescriptions = $descriptions;
        $this->includeKeywords = $keywords;
        $this->includeResources = $resources;
        $this->includeAccessoryProducts = $accessories;
        $this->includeSimilarProducts = $similar;
        $this->includeUpsellProducts = $upsells;
        $this->categoryDisplayAttributes = $cda;
        $this->flatten_category_display_attributes();
        
        if($autoPopulateProductInfo==TRUE)
        {
            //If the product needs to populate it's data itself
            
            $this->populate_product_specs();
            //Populate the technical specs 
            
            if($this->includeDescriptions)$this->populate_descriptions();
            if($this->includeKeywords)$this->populate_keywords();
            if($this->includeResources)$this->populate_resources();
            
            //if the addons are required as well
            if($upsells==True)
            {
                $this->populate_upsellProducts();
            }
            if($similar==TRUE)
            {
                $this->populate_similarProducts();
            }
            if($accessories==True)
            {
                $this->populate_accessoryProducts();
            }
            
            
        }
        
    }
    
    public function __set($name, $value) {
        
    //This method is called whenever a property is 
    //set for the object - in order to support 
    //Dynamic assignment - if the property doesn't exist
    //It needs to be added to the local_data array
        
        if($name<>"")
        {
            //if the property exists - set it 
            if(property_exists($this, $name))
            {
                $this->$name = $value;
            }
            else
            {
             //It's a dynamic property and should 
             //be assigned to the local_data array
                $this->local_data[$name]=$value;
            }
            return true;
        }
        else
        {
            return false;
        }
        
    }
    
    public function __get($name)
    {
        if($name<>"")
        {
            if(property_exists($this, $name))
            {
                //If this is an actual property 
                return $this->$name;
            }
            else
            {
                //If it's not an actual property 
                //check to see if it's available in the 
                //local_data array
                if(isset($this->local_data[$name]))
                {
                    return $this->local_data[$name];
                }
                else
                {
                    return "";
                }
                
            }
        }
    }
    
    
    private function flatten_category_display_attributes()
    {
        /**
         *
         * Flattens the category display attributes in order to easily find and map
         * an attribute to it's category display array 
         * This is helpful in reducing the number of loops needed to traverse through 
         * the multi-dimentional $cda
         * The result will be used when assigning attribute display values to the category 
         * display attribute array while keeping the order intact
         *
         * @param    None
         * @return   Nothing
         *
         */
        
                foreach($this->categoryDisplayAttributes as $templateId=>$headerId)
                {
                    
                    foreach ($headerId as $headerId=>$headerName)
                    {
                        
                        foreach ($headerName as $headerName=>$attributeId)
                        {
                           foreach($attributeId as $attributeId=>$attributeName)
                           {
                                
                               foreach($attributeName as $attributeName=>$value){
                                        $this->flatCategoryDisplayAttributes[$templateId."|".$headerId."|".$headerName."|".$attributeName."|".$attributeId]=$attributeId;
                                   
                               }
                               
                           }
                          
                        }
                    }
                }

    }
    
    private function populate_product_specs() {
        /**
         *
         * Populates the $productInformation with the technical specs
         * Expects to be used by get_technical_specs (Where the template type would be used)
         * or by get_overview where just the overview will be returned
         *
         */
        $attributeArray = array();
        
        $query = "select pa.productid as productId,
                  pa.categoryid as categoryId,
                  pa.attributeid as attributeId,
                  pa.displayvalue as displayValue
                  from ".Spex_DB_name.".productattribute pa
                  where pa.productid = ".$this->productId."
                  and localeid = ".$this->localeId
                  ; 
  
        if(!$result = query($query))
        {
            return false;
        }
        else
        {
            while ($row = mysql_fetch_assoc($result)) 
            {
                $attributeArray[$row['attributeId']][]=$row['displayValue'];
            }
           
            $this->set_techspecs($attributeArray);
            return true;
        }
    }
    private function populate_resources(){
        /**
         *
         * Populate the images based on $image_set which represents the basic or RC set of images
         * @return   True / False
         *
         */
        $resourceArray=Array();

        if(Images=='Basic')
        {
            $query = "Select productId ProductId,
                    type Type from ".Spex_DB_name.".productimages
                    where productid = $this->productId";
            if(!$result = query($query))
            {
                return FALSE;
            }
            else
            {
                while($row =  mysql_fetch_assoc($result))
                {
                    //The text based types 'Thumbnail' and 'Large' can be skipped
                    if($row['Type']!='Thumbnail'&&$row['Type']!='Large')
                    {
                    $resourceArray[$row['Type']] = $row['Type'];
                    }
                }
            }
        }
        elseif(Images=='RC')
        {
            
            $query = "Select productid ProductId,
                    type Type from ".Spex_DB_name.".productimages 
                    where productid = $this->productId 
                    UNION Select productid ProductId,
                    type Type from ".Spex_DB_name.".productimages_RC
                    where productid = $this->productId";
            if(!$result = query($query))
            {
                return FALSE;
            }
            else
            {
                
                while($row=  mysql_fetch_assoc($result)){
                    //The text based types 'Thumbnail' and 'Large' can be skipped
                    if($row['Type']!=='Thumbnail'&&$row['Type']!=='Large')
                    {
                    $resourceArray[$row['Type']] = $row['Type'];
                    }
                }
            }
            
        }
        $this->set_resources($resourceArray);
    }
    private function populate_keywords()
    {
         /*
         *
         * Populate the keywprds for the current product from the 
         * DB
         * @return   True / False
         *
         */
        $keywords="";
        
        $query = "Select keywords
                  from ".Spex_DB_name.".productkeywords
                  where productid = $this->productId
                  and localeid=".Locale;
        
            if(!$result = query($query))
            {
                return FALSE;
            }
            else
            {
                
                while($row=mysql_fetch_assoc($result))
                {
                    $keywords=$row['keywords'];
                }   
            }
            $this->set_keywords($keywords);
            return true;
     }
    
    private function populate_descriptions()
    {
        /*
         *
         * Populate the descriptions for the current product
         * @return   True / False
         *
         */
        $descriptionsArray=Array();
        
        $query = "Select type as Type,
                  description as Description
                  from ".Spex_DB_name.".productdescriptions
                  where productid = $this->productId
                  and localeid=".Locale;
        
            if(!$result = query($query))
            {
                return FALSE;
            }
            else
            {
                
                while($row=mysql_fetch_assoc($result))
                {
                    $descriptionsArray[$row['Type']]=$row['Description'];
                }   
            }
            $this->set_descriptions($descriptionsArray);
            return true;
    }
    private function populate_upsellProducts()
    {
        /*
         *
         * Populate the upsells for the current product
         * @return   True / False
         *
         */
        $upsellArray = Array();
        
        $query = "Select pu.upsellproductid as UpsellproductId,
                  pd2.description as Upselldescription,
                  pi.productid as UpsellImage 
                  from ".Spex_DB_name.".productupsell pu 
                  join productdescriptions pd2 on 
                  pu.upsellproductid = pd2.productid and 
                  pd2.type = 2 and pd2.localeid = ".Locale.
                  " join productimages pi on 
                  pu.upsellproductid = pi.productid and 
                  pi.type = '160' where pu.productid = ".$this->productId;;
        
            if(!$result = query($query))
            {
                return FALSE;
            }
            else
            {
                
                while($row=mysql_fetch_assoc($result))
                {
                    $upsellArray[$row['UpsellproductId']]['URL']="www.yahoo.com";
                    $upsellArray[$row['UpsellproductId']]['Description']=$row['Upselldescription'];
                    $upsellArray[$row['UpsellproductId']]['Image']= $this->get_resource_url('160',"", $row['UpsellproductId'], TRUE);
                }   
                
                $this->set_upsellProducts($upsellArray);
                return true;
                
            }
    }
    
    private function populate_similarProducts()
    {
        /*
         *
         * Populate the similar products for the current product
         * @return   True / False
         *
         */
        $similarArray = Array();
        
        $query = "Select ps.similarproductid as SimilarproductId,
                  pd2.description as Similardescription,
                  pi.productid as SimilarImage 
                  from ".Spex_DB_name.".productsimilar ps 
                  join productdescriptions pd2 on 
                  ps.similarproductid = pd2.productid and 
                  pd2.type = 2 and pd2.localeid = ".Locale.
                  " join productimages pi on 
                  ps.similarproductid = pi.productid and 
                  pi.type = '160' where ps.productid = ".$this->productId;
        
            if(!$result = query($query))
            {
                return FALSE;
            }
            else
            {
                
                while($row=mysql_fetch_assoc($result))
                {
                    $similarArray[$row['SimilarproductId']]['URL']="";
                    $similarArray[$row['SimilarproductId']]['Description']=$row['Similardescription'];
                    $similarArray[$row['SimilarproductId']]['Image']= $this->get_resource_url('160',"", $row['SimilarproductId'], TRUE);
                }   
                
                $this->set_similarProducts($similarArray);
                return true;
                
            }
    }
    
    private function populate_accessoryProducts()
    {
        /*
         *
         * Populate the accessory products for the current product
         * @return   True / False
         *
         */
        $accessoryArray = Array();
        
        $query = "Select pa.accessoryproductid as accessoryProductId,
                  pd2.description as accessoryDescription,
                  pi.productid as accessoryImage 
                  from ".Spex_DB_name.".productaccessories pa 
                  join productdescriptions pd2 on 
                  pa.accessoryproductid = pd2.productid and 
                  pd2.type = 2 and pd2.localeid = ".Locale.
                  " join productimages pi on 
                  pa.accessoryproductid = pi.productid and 
                  pi.type = '160' where pa.productid =".$this->productId;
        
            if(!$result = query($query))
            {
                return FALSE;
            }
            else
            {
                
                while($row=mysql_fetch_assoc($result))
                {
                    $accessoryArray[$row['accessoryProductId']]['URL']="";
                    $accessoryArray[$row['accessoryProductId']]['Description']=$row['accessoryDescription'];
                    $accessoryArray[$row['accessoryProductId']]['Image']= $this->get_resource_url('160',"", $row['accessoryProductId'], TRUE);
                }   
                
                $this->set_accessoryProducts($accessoryArray);
                return true;
                
            }
    }
    public function set_techspecs($techspecs = array())
    {
        /**
         *
         * Set the technical specs for the product - And create the formatted techspecs and overview 
         * 
         * @param    $techspecs - This must be an array containing the attributeID and the displayvalue 
         *                        the convention should be array(attributeId=>array(value)) -
         *                        There is no need to specify '32%' or '34%' in the queries 
         *                        Since both are needed to create the technical specs in the case
         *                        of Template type 1 and in the case of Template Type 0 just the 
         *                        basic set will be used
         */
        
        $attribute_set = "";
        
        if(count($techspecs)==0)
        {
            return false;
        }
        else
        {
            
            foreach ($techspecs as $attributeId=>$value) 
            {
                //locate the corresponding mapping from the flatCDA array
                $flatTemplateArray = explode('|',array_search($attributeId, $this->flatCategoryDisplayAttributes));
                
                if($flatTemplateArray[0]<>'') //did not find any match for the attributeId
                {
                    list($templateType,$headerId, $headerName, $attributeName,$attributeId) = $flatTemplateArray;
                    foreach($value as $key=>$value)
                    {
                        if(isset($this->categoryDisplayAttributes[$templateType][$headerId][$headerName][$attributeId][$attributeName]))
                        {   
                            //Check to see if the value has already been assigned for the same attribute with the same value
                            //This could happen when the values are repeated when technical specs are part of a join with another table
                            if(!is_numeric(array_search($value,$this->categoryDisplayAttributes[$templateType][$headerId][$headerName][$attributeId][$attributeName])))
                            {        
                            $this->categoryDisplayAttributes[$templateType][$headerId][$headerName][$attributeId][$attributeName][] = $value;
                            }
                        }
                        else
                        {
                            $this->categoryDisplayAttributes[$templateType][$headerId][$headerName][$attributeId][$attributeName][]=$value;
                        }
                        
                    }
                }
                
            }
            
            //Assign attribute values according to the sort order maintained by categoryDisplayAttributes
            //and remove any attribute that doesn't have a value
                    foreach($this->categoryDisplayAttributes as $templateType=>$headerId)
                    {
                        foreach($headerId as $headerId=>$headerName)
                        {
                            foreach($headerName as $headerName=>$attributeId)
                            {

                                foreach($attributeId as $attributeId=>$attributeName)
                                {

                                    foreach($attributeName as $attributeName=>$value)
                                    {
                                        foreach($value as $key=>$value)
                                        {
                                            if(isset($value)){
                                                $this->productInformation['attributes'][$templateType][$headerId][$headerName][$attributeId][$attributeName][]=$value;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
            
            
            //use the available information to create the formatted technical specs
            $this->format_techspecs();
            //use the available information to create the formatted Overview
            $this->format_overview();
            
            //unset the attributes array since it's no longer needed 
            if(isset($this->productInformation['attributes'])) unset($this->productInformation['attributes']);
            //unset the categoryDisplayAttributes which is no more required
            if(isset($this->categoryDisplayAttributes))unset($this->categoryDisplayAttributes);
            //unset the flatCategoryDisplayAttributes since it's no more required
            if(isset($this->flatCategoryDisplayAttributes))unset($this->flatCategoryDisplayAttributes);
            
            return true;
        }
        
    }
    
    protected function format_techspecs()
    {
        //sets the formatting for the technical specs, assigns the attribute values
        //to the attribute_data variable and converts 
        //the productInformation array into a string (This is useful in order to avoid
        //the productInformation array from hogging memory)
  

        
                if ($this->templateId == 1) 
                {
                    $attribute_set = $this->productInformation['attributes'][1];
                } else {
                    $attribute_set = $this->productInformation['attributes'][0];
                    
                }

                foreach ($attribute_set as $headerId => $headerName) 
                { 

                    foreach ($headerName as $headerName => $attributes) 
                    {
                        $this->techspecs.= "<div style=\"font-size:12px;margin:5px 0 5px 0\"><b>" . $headerName . "</b><hr>";
                            foreach ($attributes as $attributeId => $attribute) 
                            {
                                foreach ($attribute as $attributeName => $value) 
                                {
                                    if (count($value) == 1) 
                                    {
                                        $this->techspecs.= "<span style='color:#333333'>" . $attributeName . "</span>:  " . $value[0] . "<br/>";
                                        //Dynamically set attributes that would be returned if requested by get_attribute
                                        $this->attribute_data[$attributeId] = $value[0];
                                    } 
                                    else 
                                    {
                                        $this->techspecs.= "<span style='color:#333333'>" . $attributeName . "</span>:  ";
                                        foreach ($value as $value) 
                                        {
                                            $this->techspecs.= "<li>" . $value . "</li>";
                                            if(isset($this->attribute_data[$attributeId]))
                                            {
                                                $this->attribute_data[$attributeId] .= "<li>" . $value . "</li>";
                                                //for repeatable values - if it has been already set 
                                            }
                                            else
                                            {
                                                $this->attribute_data[$attributeId] = "<li>" . $value . "</li>";
                                            }
                                            
                                        }
                                    }
                                }
                            }
                    }
                    if($this->techspecs<>"")
                    {
                    $this->techspecs.= "</div>";
                    }
                }
    }
    
    protected function format_overview()
    {
        //Sets the overview according to pre-defined 
        //formatting and criteria of what the overview
        //should consist of 
                
                
                if (!isset($this->productInformation['attributes'][0])) 
                {
                    $this->overview="";
                }
                else
                {
                    
                       $marketingText = '';
                       $overView = '';
                       foreach ($this->productInformation['attributes'][0] as $headerId => $headerName) 
                       {
                            //Only the basic set is needed for the overview 

                            if ($headerId <> 35) 
                            { //Only attributes falling under headers other than General information need to qualify 
                                
                                foreach ($headerName as $headerName => $attributes) 
                                {
                                    foreach ($attributes as $attributeId => $attributeName) 
                                    {
                                        //if the attribute is Marketing Text
                                            foreach($attributeName as $attributeName => $value)
                                            {
                                                if (count($value) == 1) 
                                                {
                                                    $overView.= "<li>" . $attributeName . ":  " . str_replace('</li>', ',', str_replace("<li>", "", $value[0])) . "</li>";
                                                } 
                                                else 
                                                {
                                                    $overView.= "<li>" . $attributeName . ": " . implode(', ', $value) . "</li>";
                                                }

                                            }
                                    }
                                }
                            } 
                        }        
                                
                        //Get the Marketing information separately
                        if(Product_Template==0)
                        {
                        $marketingText = $this->get_attribute(341452);
                        }
                        else
                        {
                            $marketingText = $this->get_attribute(321452);
                        }
                           
                        
                        if($marketingText<>"")
                        {
                            $this->overview.= "<br/><b>Product Description</b>".$marketingText."<br/>".$overView;
                        }
                        else
                        {
                            $this->overview.=$overView;   
                        }
                }
    }
    
    
    public function set_resources($resources = array())
    {
        /**
         *
         * Set the images / resources for the product - This is to be used when the caller is passing
         * the images / resources as opposed to expecting the product to fetch the images / resources from 
         * the DB
         * @param    $resources - This must be an array containing the image Type and the displayvalue 
         *                        the convention should be array(Type)
         */
        
        if(count($resources)==0)
        {
            return false;
        }
        else
        {
            foreach($resources as $key=>$value)
            {
                    //The text based types 'Thumbnail' and 'Large' can be skipped
                    if($value!=='Thumbnail'&&$value!=='Large')
                    {
                    $this->resources[$value]= (string)$value;
                    }
            }
        }
    }
    public function set_descriptions($descriptions = array())
    {
        /**
         *
         * Set the descriptions for the product - This method would be used by the caller to set 
         * the descriptions as opposed to expecting the product to fetch the descriptions from 
         * the DB
         * @param    $descriptions - This must be an array containing the Type and the description 
         *                        the convention should be array(Type=>description)
         */
        
        foreach($descriptions as $key=>$value)
        {
            $this->descriptions[$key]=$value;
        }
    }
    public function set_keywords($keywords)
    {
        /**
         *
         * Set the keywords for the product - This method would be used by the caller to set 
         * the keywords as opposed to expecting the product to fetch the keywords from 
         * the DB
         * @param    $keywords - This must be a string containing the keywords for the product 
         */
        
        $this->keywords = $keywords;
        return true;
    }
    public function set_upsellProducts($upsellArray = Array())
    {
        /**
         *
         * Set the upsells for the product - This method would be used by the caller to set 
         * the upsells as opposed to expecting the product to fetch the upsells from 
         * the DB
         * @param    $upsellArray - This must be an array containing the following:
         *                          Array(ProductCode=>Array('URL'=>'UpsellProductURL',
         *                                                   'Description'=>'UpsellProductDescription'
         *                                                   'Image'=>'UpsellProductImage'))
         */
        
        $upsellcounter = 0;
        
        If(count($upsellArray)==0)
        {
            return false;
            
        }
        else
        {
            //format and set the upsell variable with markup 
            $upsells = "<table><tr>";
            foreach($upsellArray as $productCode=>$data)
            {
                
                if($upsellcounter==UpsellProducts) break;
                    $upsells.="<td><a href=".$data['URL']."><img src='".$data['Image']."'/><p>".$data['Description']."</p></a></td>";
                $upsellcounter++;
            }
            $upsells .= "<tr/></table>";
            $this->upsellFormatted = $upsells;
            return true;
        }
    }
    
    public function set_similarProducts($similarArray = Array())
    {
        /**
         *
         * Set the similar products for the product - This method would be used by the caller to set 
         * the similar items as opposed to expecting the product to fetch the similar items from 
         * the DB
         * @param    $similarArray - This must be an array containing the following:
         *                          Array(ProductCode=>Array('URL'=>'SimilarProductURL',
         *                                                   'Description'=>'SimilarProductDescription'
         *                                                   'Image'=>'SimilarProductImage'))
         */
        $similarcounter = 0;
        If(count($similarArray)==0)
        {
            return false;
        }
        else
        {
            //format and set the upsell variable with markup 
            $similar = "<table><tr>";
            foreach($similarArray as $productCode=>$data)
            {
                if($similarcounter==SimilarProducts) break;
                    $similar.="<td><a href=".$data['URL']."><img src='".$data['Image']."'/><p>".$data['Description']."</p></a></td>";
                $similarcounter++;
            }
            $similar .= "<tr/></table>";
            $this->similarFormatted = $similar;
            return true;
            
        }
    }
    
    public function set_accessoryProducts($accessoryArray = Array())
    {
        /**
         *
         * Set the accessory products for the product - This method would be used by the caller to set 
         * the accessory items as opposed to expecting the product to fetch the accessory items from 
         * the DB
         * @param    $accessoryrray - This must be an array containing the following:
         *                          Array(ProductCode=>Array('URL'=>'AccessoryProductURL',
         *                                                   'Description'=>'AccessoryProductDescription'
         *                                                   'Image'=>'AccessoryProductImage'))
         */
        $accessoryCounter=0;
        If(count($accessoryArray)==0)
        {
            return false;
        }
        else
        {
            //format and set the accessories variable with markup 
            $accessories = "<table><tr>";
            foreach($accessoryArray as $productCode=>$data)
            {
                if($accessoryCounter==AccessoryProducts) break;
                    $accessories.="<td><a href=".$data['URL']."><img src='".$data['Image']."'/><p>".$data['Description']."</p></a></td>";
                $accessoryCounter++;
            }
            $accessories .= "<tr/></table>";
            $this->accessoriesFormatted = $accessories;
            return true;
        }
    }
    
    
    public function get_keywords()
    {
        /**
         *
         * Get the assigned keywords for the product
         * @param    None
         * Returns   String
         */
        return $this->keywords;
    }
    
    public function get_upsellProducts()
    {
        /**
         *
         * Returns the upsell products - configured by set_upsell
         * 
         * @return   Array
         *
         */
        if(isset($this->upsellFormatted))
        {
            return $this->upsellFormatted;
        }
        else
        {
            return "";
        }
    }
    
    public function get_similarProducts()
    {
        /**
         *
         * Returns the similar products for the base product 
         * The number is determined from the configuration
         *
         * @return   Array
         *
         */
      if(isset($this->similarFormatted))
      {
          return $this->similarFormatted;
      }
      else
      {
          return "";
      }
    }
    
    public function get_accessoryProducts()
    {
        /**
         *
         * Returns the accessory products 
         * This would be based on the value in the configuration
         *
         * @return   Array
         *
         */
        if(isset($this->accessoriesFormatted))
        {
            return $this->accessoriesFormatted;
        }
        else
        {
            return "";
        }
    }

    public function get_techspecs() 
    {
        /**
         *
         * Returns Technical specs based on Template Type
         *
         * @return   String 
         *
         */
        if (!isset($this->techspecs)) {
            
            return false;
            
        }
        else
        {
            return $this->techspecs;
        }
    }

    public function get_product_overview() {
        /**
         *
         * Returns overview or main features and Marketing text
         * @return   String 
         *
         */
        if (!isset($this->overview)) {
            return false;
        }
        else
        {
            return $this->overview;
        }

        
    }
    
    public function get_attribute($attributeId=0){
        //Function to return single attribute values
        //This would be helpful in getting single attributes 
        //when needed 
        if($attributeId==0&&!isset($attributeId))
        {
            return "";
        }
        else
        {
            if(isset($this->attribute_data[$attributeId]))
            {
                return $this->attribute_data[$attributeId];
            }
            else
            {
                return "";
            }
        }
        
    }
    
    public function get_description($type)
    {

        if(!isset($this->descriptions[$type]))
        {
            return "";
        }
        else
        {
            return $this->descriptions[$type];
            return true;
        }
    }
    public function get_resource($type='160', $custom_size="", $only_published=TRUE, $append_logo=TRUE)
    {
        /**
         *
         * Resource URL for requested type
         *
         * @param $type - The type of resource 
         * @param $custom_size - The custom size - this should be passed as "width/height"
         * @param $only_published - Will only return an image URL if the type is published
         * @param $append_logo - will append noimage=logo at the end of the URL
         *
         * @return   String 
         *
         */
        
        if($only_published==TRUE&&$type<>'')
        {
            //Check if the product image is published 
            if(!isset($this->resources[$type]))
            {
                
                return "";
            }
            else
            {
                return $this->get_resource_url($type, $custom_size, $this->productId, $append_logo);
            }
        }
        else
        {
            return $this->get_resource_url($type, $custom_size, $this->productId, $append_logo);
        }
        
    }
    public function get_resource_url($type='',$custom_size="",$productId=0,$append_logo=TRUE)
    {
        /**
         *
         * Creates the resource URL according to the type specified 
         *
         * @param $type - The type of resource 
         * @param $custom_size - The custom size - this should be passed as "width/height"
         * @param $productId - The productId for the URL - keep in mind this is not responsible for checking if the product 
         *                     image is published - This would need to be verified by the caller
         * @param $append_logo - will append noimage=logo at the end of the URL
         *
         * @return   String 
         *
         */
        
        $URL = "";
        $types = array(
            "Thumbnail" =>  ".jpg",
            "Top"       =>  ".jpg",
            "300"       =>  ".jpg",
            "500"       =>  ".jpg",
            "1300"      =>  ".jpg",
            "left"      =>  ".jpg",
            "1100"      =>  ".jpg",
            "Right"     =>  ".jpg",
            "650"       =>  ".jpg",
            "Rear"      =>  ".jpg",
            "Large"     =>  ".jpg",
            "160"       =>  ".jpg",
            "225"       =>  ".jpg",
            "Original"  =>  ".jpg",
            "Front"     =>  ".jpg",
            "75"        =>  ".jpg",
            "900"       =>  ".jpg",
            "Bottom"    =>  ".jpg",
            "Life-Style"=>  ".jpg",
            "Jack-Pack" =>  ".jpg",
            "User-Manual"=>  ".pdf",
            "Manufacturer-Brochure" =>  ".pdf",
            "Assembly-Instructions" =>  ".pdf",
            "Tour"      =>  ".html"
            );
        
        if($type==""&&$custom_size=="")
        {
            return "";
        }
        if($type<>'') //If a type has been specified 
        {
            
            if($types[$type]=='.jpg') //If the Type corresponds to a JPG
            {
                    if($custom_size<>"") //If a custom size has been specified 
                    {
                        $URL =  "http://content.etilize.com/images/".$custom_size."/$productId.jpg";
                    }
                    else
                    {
                        $URL =  "http://content.etilize.com/$type/$productId.jpg";
                    }
                    if($append_logo==TRUE)
                    {
                        $URL.="?noimage=logo";
                    }
              return $URL;      
            }
            elseif($types[$type]=='.pdf') //if the Type corresponds to a PDF
            {
                $URL =  "http://content.etilize.com/$type/$productId.pdf";
              
              return $URL;  
            }
            elseif($types[$type]=='.html') //if the Type corresponds to a page 
            {
                $URL =  "http://content.etilize.com/$type/$productId/index.html";
              
              return $URL;
            }
            else
            {
                return "";
            }
        }
        else            //If a custom size has been requested
        {
            
            $URL = "http://content.etilize.com/images/$custom_size/$productId.jpg";
            if($append_logo==TRUE)
                    {
                        $URL.="?noimage=logo";
                    }
            return $URL;
        }
    }
    public function get_largest_image()
    {
        /**
         *
         * Resource URL for requested type
         *
         * @param $type - The type of resource 
         * @param $custom_size - The custom size - this should be passed as "width/height"
         *
         * @return   String 
         *
         */
        $images = array();
        
        if(count($this->resources)==0){
            return "";
        }
        else
        {
            foreach($this->resources as $image)
            {
                if((int)$image>0) //images will be set to 0 if textual
                {
                    $images[]=(int)$image;
                }
            }
            array_multisort($images,SORT_DESC);
            if(!isset($images[0]))
            {
                return "";
            }
            else
            {
                return $this->get_resource($images[0]);
            }
            
        }
        
    }
    
    
}

?>
